<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;

/**
 * 验证接口
 */
class Validate extends Api
{
    protected $noNeedLogin = '*';
    protected $layout = '*';
    protected $error = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    public function add($data)
    {
        $validate = new \think\Validate([
            ['name', 'require', '请填写行程名称'],
            ['route_id', 'require', '请填写行程线路'],
            ['starttime', 'require|date', '请填写开始时间'],
            ['overtime', 'require|date', '请填写结束时间'],
            ['Trips', 'require', '请填写行程车次'],
            ['number', 'require|number', '旅行团人数|旅行团人数只能填写纯数字'],

        ]);
        if (!$validate->check($data)) {
            $this->error('提交失败：' . $validate->getError());
       }
    }
    public function UserInfo($data)
    {
        $validate = new \think\Validate([
            ['nickname', 'require', '请填写昵称'],
            ['headerimg', 'require', '请填写头像'],
            ['name', 'require', '请填写姓名'],
            ['age', 'require', '请填写年龄'],
            ['gender', 'require', '请填写性别'],
            ['phone', 'require', '手机号'],
            ['region', 'require', '地区'],
            ['address', 'require', '详细地址'],

        ]);
        if (!$validate->check($data)) {
            $this->error('提交失败：' . $validate->getError());
       }
    } 
    public function bind($data)
    {
        $validate = new \think\Validate([
            ['user_id', 'require', '请填写游客ID'],
            ['id', 'require', '请填写行程ID'],
           

        ]);
        if (!$validate->check($data)) {
            $this->error('提交失败：' . $validate->getError());
       }
    }
     public function del($data)
    {
        $validate = new \think\Validate([
            ['user_id', 'require', '请填写摄影师ID'],
            ['id', 'require', '请填写行程ID'],
           

        ]);
        if (!$validate->check($data)) {
            $this->error('提交失败：' . $validate->getError());
       }
    }
    public function update($data)
    {
        $validate = new \think\Validate([
            ['city_id', 'require', '请填写城市ID'],
            ['itinerary_id', 'require', '请填写行程ID'],
            ['user_id', 'require', '请填写游客ID'],
            ['file_id', 'require', '请填写图片ID'],
           

        ]);
        if (!$validate->check($data)) {
            $this->error('提交失败：' . $validate->getError());
       }
    }
    public function detailed($data)
    {
        $validate = new \think\Validate([
            ['id', 'require', '请填写行程ID'],
           

        ]);
        if (!$validate->check($data)) {
            $this->error('提交失败：' . $validate->getError());
       }
    }
    public function Itinerary($data){
        $validate = new \think\Validate([
            ['user_id', 'require', '请填写摄影师ID'],
           

        ]);
        if (!$validate->check($data)) {
            $this->error('提交失败：' . $validate->getError());
       }  
    }

    /**
     * 检测邮箱
     *
     * @ApiMethod (POST)
     * @param string $email 邮箱
     * @param string $id    排除会员ID
     */
    public function check_email_available()
    {
        $email = $this->request->post('email');
        $id = (int)$this->request->post('id');
        $count = User::where('email', '=', $email)->where('id', '<>', $id)->count();
        if ($count > 0) {
            $this->error(__('邮箱已经被占用'));
        }
        $this->success();
    }

    /**
     * 检测用户名
     *
     * @ApiMethod (POST)
     * @param string $username 用户名
     * @param string $id       排除会员ID
     */
    public function check_username_available()
    {
        $username = $this->request->post('username');
        $id = (int)$this->request->post('id');
        $count = User::where('username', '=', $username)->where('id', '<>', $id)->count();
        if ($count > 0) {
            $this->error(__('用户名已经被占用'));
        }
        $this->success();
    }

    /**
     * 检测昵称
     *
     * @ApiMethod (POST)
     * @param string $nickname 昵称
     * @param string $id       排除会员ID
     */
    public function check_nickname_available()
    {
        $nickname = $this->request->post('nickname');
        $id = (int)$this->request->post('id');
        $count = User::where('nickname', '=', $nickname)->where('id', '<>', $id)->count();
        if ($count > 0) {
            $this->error(__('昵称已经被占用'));
        }
        $this->success();
    }

    /**
     * 检测手机
     *
     * @ApiMethod (POST)
     * @param string $mobile 手机号
     * @param string $id     排除会员ID
     */
    public function check_mobile_available()
    {
        $mobile = $this->request->post('mobile');
        $id = (int)$this->request->post('id');
        $count = User::where('mobile', '=', $mobile)->where('id', '<>', $id)->count();
        if ($count > 0) {
            $this->error(__('该手机号已经占用'));
        }
        $this->success();
    }

    /**
     * 检测手机
     *
     * @ApiMethod (POST)
     * @param string $mobile 手机号
     */
    public function check_mobile_exist()
    {
        $mobile = $this->request->post('mobile');
        $count = User::where('mobile', '=', $mobile)->count();
        if (!$count) {
            $this->error(__('手机号不存在'));
        }
        $this->success();
    }

    /**
     * 检测邮箱
     *
     * @ApiMethod (POST)
     * @param string $mobile 邮箱
     */
    public function check_email_exist()
    {
        $email = $this->request->post('email');
        $count = User::where('email', '=', $email)->count();
        if (!$count) {
            $this->error(__('邮箱不存在'));
        }
        $this->success();
    }

    /**
     * 检测手机验证码
     *
     * @ApiMethod (POST)
     * @param string $mobile  手机号
     * @param string $captcha 验证码
     * @param string $event   事件
     */
    public function check_sms_correct()
    {
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        $event = $this->request->post('event');
        if (!\app\common\library\Sms::check($mobile, $captcha, $event)) {
            $this->error(__('验证码不正确'));
        }
        $this->success();
    }

    /**
     * 检测邮箱验证码
     *
     * @ApiMethod (POST)
     * @param string $email   邮箱
     * @param string $captcha 验证码
     * @param string $event   事件
     */
    public function check_ems_correct()
    {
        $email = $this->request->post('email');
        $captcha = $this->request->post('captcha');
        $event = $this->request->post('event');
        if (!\app\common\library\Ems::check($email, $captcha, $event)) {
            $this->error(__('验证码不正确'));
        }
        $this->success();
    }
}
